<template>
    <div class="user">
        <!-- <div class="btns">
            <el-button @click="roleFormVisible = true" size="small" type="primary" icon="el-icon-plus">新增</el-button>
        </div> -->
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="用户编号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="CRP号" width="180">
            </el-table-column>
            <el-table-column prop="realName" label="用户姓名" width="180">
            </el-table-column>
            <el-table-column prop="departmentId" label="部门" width="180">
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="180">
            </el-table-column>
            <!-- 权限字符 -->
            <el-table-column prop="isActive" label="状态">
                <template slot-scope="scope">
                    <el-switch active-color="#13ce66" inactive-color="#1890FF">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="创建时间">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <el-dialog title="添加用户" :visible.sync="roleFormVisible">
            <el-form :rules="rules" ref="form" :model="form">
                <el-form-item label="用户名称" :label-width="formLabelWidth">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="form.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="真实姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.realName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" :label-width="formLabelWidth">
                    <el-input v-model="form.gender" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="用户昵称" :label-width="formLabelWidth">
                    <el-input v-model="form.nickname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" :label-width="formLabelWidth">
                    <el-input v-model="form.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="归属部门" :label-width="formLabelWidth">
                    <el-input v-model="form.departmentId" autocomplete="off"></el-input>
                    <!-- <el-select v-model="form.region" placeholder="请选择部门">
                        <el-option label="学工处" value="shanghai"></el-option>
                        <el-option label="学生科" value="beijing"></el-option> 
                    </el-select> -->
                </el-form-item>
                <el-form-item label="角色" :label-width="formLabelWidth">
                    <el-input v-model="form.roleId" autocomplete="off"></el-input>
                    <!-- <el-select v-model="form.role" placeholder="请选择角色">
                        <el-option label="超级管理员" value="shanghai"></el-option>
                        <el-option label="学生" value="beijing"></el-option>  gender
                    </el-select> -->
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="form.isActive">
                        <el-radio label="正常"></el-radio>
                        <el-radio label="停用"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button @click="roleFormVisible = false">取 消</el-button>
                    <el-button :disabled="submitting" type="primary" @click="submitForm('form')">确 定</el-button>
                </el-form-item>
            </el-form>
            <!-- <div slot="footer" class="dialog-footer">
            </div> -->
        </el-dialog>

        <el-dialog title="修改用户" :visible.sync="editFormVisible">
            <el-form :rules="rules" ref="formEdit" :model="formEdit">
                <el-form-item label="用户名称" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="用户昵称" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.nickname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="归属部门" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.departmentId" autocomplete="off"></el-input>
                    <!-- <el-select v-model="form.region" placeholder="请选择部门">
                        <el-option label="学工处" value="shanghai"></el-option>
                        <el-option label="学生科" value="beijing"></el-option>  roleId
                    </el-select> -->
                </el-form-item>
                <el-form-item label="角色" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.roleId" autocomplete="off"></el-input>
                    <!-- <el-select v-model="form.role" placeholder="请选择角色">
                        <el-option label="超级管理员" value="shanghai"></el-option>
                        <el-option label="学生" value="beijing"></el-option>
                    </el-select> -->
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="formEdit.isActive">
                        <el-radio label="正常"></el-radio>
                        <el-radio label="停用"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button @click="editFormVisible = false">取 消</el-button>
                    <el-button :disabled="submitting2" type="primary" @click="submitEditForm('formEdit')">确 定</el-button>
                </el-form-item>
            </el-form>
            <!-- <div slot="footer" class="dialog-footer">
            </div> -->
        </el-dialog>
    </div>
</template>

<script>
import { addUser, delUser, listUser, editUser } from '@/api/authority';

export default {
    data() {
        return {
            //初始化分页数据
            currentPage: 1, // 当前页码
            pageSize: 10,   // 每页显示的记录数
            total: 0   ,    // 总记录数
            roleFormVisible: false,
            editFormVisible:false,
            submitting: false,
            submitting2: false,
            tableData:[], 
            // [{
            //     id: '1',
            //     name: '张三',
            //     nickname: "admin",
            //     phone: "13254785879",
            //     dept: "学工处人员",
            //     role: "普通",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '张三',
            //     nickname: "admin",
            //     phone: "13254785879",
            //     dept: "学工处人员",
            //     role: "普通",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '张三',
            //     nickname: "admin",
            //     phone: "13254785879",
            //     dept: "学工处人员",
            //     role: "普通",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '张三',
            //     nickname: "admin",
            //     phone: "13254785879",
            //     dept: "学工处人员",
            //     role: "普通",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '张三',
            //     nickname: "admin",
            //     phone: "13254785879",
            //     dept: "学工处人员",
            //     role: "普通",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }],
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            formEdit: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            formLabelWidth: '120px',
            //权限信息对象
            authInfo: {},
            rules:{

            }
        }
    },
    mounted(){
        this.getUserList(this.currentPage,this.pageSize);
        this.getUserLength();
    },
    methods:{
        //查看用户信息表
        getUserList(pageNo,Size){
            listUser({pageNo: pageNo, size: Size}).then(res => {
                console.log("用户信息表", res)
                const data = res.data;
                this.tableData = data;
            })
        },
        getUserLength(){
            listUser({pageNo: 1, size: 100}).then(res => {
                console.log("用户总数", res.data.length)
                this.total = res.data.length;
            })
        },
         // 处理每页显示记录数的变化
         handleSizeChange(val) {
            console.log("每页显示记录数：", val)
            this.pageSize = val;
            this.getUserList(this.currentPage,this.pageSize);
        },
        // 处理当前页码的变化
        handleCurrentChange(val) {
            console.log("当前页码：", val)
            this.currentPage = val;
            this.getUserList(this.currentPage,this.pageSize);
        },
        //新增
        submitForm(formName) {
            // console.log(JSON.stringify(this.attachArr))
            // return;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitting = true;
                    addUser(this.form).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: '用户新增成功',
                                type: 'success'
                            });
                            setTimeout(() => {
                                this.roleFormVisible = false,
                                this.$router.push("/userManage")//返回的网页路径
                            })
                            this.submitting = false;
                        }
                    })
                } else {
                    return false;
                }
            })
        },
        //修改
        submitEditForm(formName) {
            // console.log(JSON.stringify(this.attachArr))
            // return;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitting2 = true;
                    editUser(this.formEdit).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: '角色修改成功',
                                type: 'success'
                            });
                            setTimeout(() => {
                                this.editFormVisible = false,
                                this.$router.push("/userManage")//返回的网页路径
                            })
                            this.submitting2 = false;
                        }
                    })
                } else {
                    return false;
                }
            })
        },
         //修改
         handleEdit(index, row){
            //显示修改表单
            this.editFormVisible = true
            this.formEdit = row
        },
        //删除
        handleDelete(index, row) {
            console.log("删除角色：参数index", index)
            console.log("删除角色：参数row", row)
            this.$confirm('确定删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser(row.id).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getUserList()
                    }
                })
            }).catch(() => { });
        }
    }
}
</script>

<style lang="scss" scoped>
.user {
    padding: 20px;

    .btns {
        margin-top: 35px;
    }

    .el-table {
        margin-top: 20px;
    }

    .btn {
    display: flex;
    justify-content: flex-end;
    }
}
</style>